package com.zb.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.client.SkuFeignClient;
import com.zb.config.MQConfig;
import com.zb.entity.TbOrderItemModel;
import com.zb.entity.TbOrderModel;
import com.zb.mapper.TbOrderMapper;
import com.zb.service.CartService;
import com.zb.service.TbOrderItemService;
import com.zb.service.TbOrderService;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName TbOrderServiceImpl
 * @Description 服务接口实现
 * @Author zj
 * @Date 2022/11/09 13:57
 **/
@Service
public class TbOrderServiceImpl extends ServiceImpl<TbOrderMapper, TbOrderModel> implements TbOrderService {

    @Autowired
    private TbOrderItemService tbOrderItemService;

    @Autowired
    private CartService cartService;

    @Autowired
    private SkuFeignClient skuFeignClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
//    @GlobalTransactional
    public Integer addOrder(TbOrderModel orderModel, String[] skuIds) {
        //获取购买的商品集合
        List<TbOrderItemModel>orderItemModels=cartService.buyList(skuIds,orderModel.getUsername());
        Integer totalNum =0;
        Integer totalMoney=0;
        orderModel.setId(String.valueOf(IdWorker.getId()));
        for (TbOrderItemModel itemModel : orderItemModels) {
            totalNum += itemModel.getNum();
            totalMoney += itemModel.getNum()*itemModel.getMoney();
            itemModel.setOrderId(orderModel.getId());
            tbOrderItemService.save(itemModel);
            //todo:远程调用修改商品库存 分布式事务的问题
            Integer goodsNum = skuFeignClient.updateGoodsNum(itemModel.getSkuId(), itemModel.getNum());
            if (goodsNum <= 0) {
                return 0;
            }
//            tbOrderItemService.save(itemModel);
        }
        orderModel.setTotalMoney(totalMoney);
        orderModel.setTotalNum(totalNum);
        orderModel.setOrderStatus("1");
        System.out.println(orderModel);
        baseMapper.insert(orderModel);//订单添加
        //清除购物车中的商品记录
        cartService.delete(skuIds,orderModel.getUsername());

//        rabbitTemplate.convertAndSend(MQConfig.ONE_EXCHANGE, MQConfig.ONE_ROUTING, JSON.toJSONString(orderModel), new MessagePostProcessor() {
//            @Override
//            public Message postProcessMessage(Message message) throws AmqpException {
//                message.getMessageProperties().setExpiration("10000");
//                return message;
//            }
//        });
        return 1;
    }
}
